# 导包
from api.login import LoginAPI
from api.course import CourseAPI
from api.contract import ContractAPI
import config


# 创建测试类
class TestContractBusiness:
    # 初始化
    token = None
    filename = None

    # 前置处理
    def setup(self):
        # 实例化接口对象
        self.login_api = LoginAPI()
        self.course_api = CourseAPI()
        self.contract_api = ContractAPI()

    # 后置处理
    def teardown(self):
        pass

    # 1、登录成功
    def test01_login_success(self):
        # 获取验证码
        res_v = self.login_api.get_verify_code()
        print(res_v.status_code)
        print(res_v.json())
        # 登录
        login_data = {
            "username": "admin",
            "password": "HM_2023_test",
            "code": "2",
            "uuid": res_v.json().get('uuid')
        }

        res_l = self.login_api.login(test_data=login_data)
        # 提取token并保存到类属性中
        TestContractBusiness.token = res_l.json().get("token")
        print(TestContractBusiness.token)
        print(res_l.status_code)
        print(res_l.json())

    # 2、课程新增成功
    def test02_add_course(self):
        add_data = {
            "name": "测试开发提升课01",
            "subject": "99",
            "price": 10000,
            "applicablePerson": "2"
        }
        response = self.course_api.course_add(test_data=add_data, token=TestContractBusiness.token)
        print(response.status_code)
        print(response.json())

    def test03_upload_contract(self):
        f = open(config.BASE_PATH + "/data/test.pdf", "rb")
        response = self.contract_api.upload_contract(test_data=f, token=TestContractBusiness.token)
        filename = TestContractBusiness.filename = response.json().get("fileName")
        print(filename)
        print(response.status_code)
        print(response.json())

    def test04_add_contract(self):
        # add_data = {
        #     "name": "测试888",
        #     "phone": "13612345678",
        #     "contractNo": "HT1003600000",
        #     # "contractNo": "HT10012003",
        #     "subject": "6",
        #     "courseId": "99",
        #     "channel": "0",
        #     "activityId": 77,
        #     "fileName": "xxx"
        # }
        # response = self.contract_api.add_contract(test_data=add_data, token=TestContractBusiness.token)
        print(TestContractBusiness.filename)
        add_data = {"name": "测试888", "phone": "13612345678", "contractNo": "HT99912003", "subject": "6",
                    "courseId": " 100", "channel": "0", "activityId": 77, "fileName": TestContractBusiness.filename}
        response = self.contract_api.add_contract(test_data=add_data, token=TestContractBusiness.token)
        print(response.json())
        print(response.status_code)
